package team.kas.anime.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.kas.anime.pojo.Result;
import team.kas.anime.pojo.Video;
import team.kas.anime.service.VideoService;
import team.kas.anime.util.DownUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @RequestMapping("/getAllVideos")
    @ResponseBody
    public Result getAllVideos(Integer page,Integer limit){
        Result result = new Result();
        page = (page-1)*limit;
        List<Video> videos = videoService.getAllVideos(page, limit);
        Integer count = videoService.getCount();
        result.setCount(count);
        result.setData(videos);
        return result;
    }

    @RequestMapping("/video")
    public String video(){
        return "/video/video";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result del(String id){
        return videoService.del(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(String id,String value,String name){
        return videoService.update(id,value,name);
    }

    @RequestMapping("/content")
    public String addVideo(){
        return "video/addVideo";
    }

    @RequestMapping("/delVideos")
    @ResponseBody
    public Result delVideos(String[] ids){

        return videoService.delVideos(ids);
    }

    @RequestMapping("/up")
    @ResponseBody
    public Result upFile(HttpServletRequest request, MultipartFile file){
        String type = "video";
        return videoService.upFile(file,request,type);
    }

    @RequestMapping("/addVideo")
    @ResponseBody
    public Result addVideo(Video video, HttpServletRequest request){
        return videoService.addVideo(video,request);
    }

    @RequestMapping("/down")
    public void down(HttpServletRequest request, HttpServletResponse response,String filename){
        String[] file = filename.split("/");
        filename = file[file.length-1];
        DownUtil.down(request,response,filename);
    }

    @RequestMapping("/searchName")
    @ResponseBody
    public Result searchName(String value,Integer page,Integer limit){
        return videoService.searchName(value,page,limit);
    }
}
